﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Shopping_List
{
    public partial class classic : Form
    {
        public classic()
        {
            InitializeComponent();
        }
        public bool callflag;
        string filename = "untitled";
        const string savestring = ("Lister_V.1.0");

        private void AddButton_Click(System.Object sender, System.EventArgs e)
        {
            if(AddButton.Text == "Add"){
                ItemList.Items.Add(InsertText.Text);
                InsertText.Text = "";
                InsertText.Focus();
            } else {
                int i = ItemList.SelectedIndex;
                ItemList.Items.Remove(ItemList.SelectedItem);
                ItemList.Items.Insert(i, InsertText.Text);
                CancelButton.Visible = false;
                AddButton.Text = "Add";
                InsertText.Text = "";
            }
        }

        private void RemoveButton_Click(System.Object sender, System.EventArgs e)
        {
            ItemList.Items.Remove(ItemList.SelectedItem);
            CancelButton.Visible = false;
            AddButton.Text = "Add";
            InsertText.Text = "";
        }

        private void CancelButton_Click(System.Object sender, System.EventArgs e)
        {
            CancelButton.Visible = false;
            AddButton.Text = "Add";
            InsertText.Text = "";
        }

        private void ItemList_DoubleClick(System.Object sender, System.EventArgs e)
        {
            if (ItemList.SelectedIndex > -1)
            {
                CancelButton.Visible = true;
                AddButton.Text = "Rename";
                InsertText.Text = ItemList.SelectedItem.ToString();
            }
        }

        private void NewButton_Click(System.Object sender, System.EventArgs e)
        {
            ItemList.Items.Clear();
        }

        private void PrintButton_Click(System.Object sender, System.EventArgs e)
        {
            
            //callflag = 1;
            //print.ShowDialog();
        }

        private void OpenButton_Click(System.Object sender, System.EventArgs e)
        {
            string tmp;
            Open.ShowDialog();
            System.IO.StreamReader reader = new System.IO.StreamReader(Open.FileName);
            tmp = reader.ReadLine();
            if (tmp == savestring)
            {
                tmp = reader.ReadLine();
                while (!reader.EndOfStream)
                {
                    ItemList.Items.Add(tmp);
                    tmp = reader.ReadLine();
                }
                reader.Close();
            }
        }

        private void SaveAs()
        {
            Save.ShowDialog();
            System.IO.StreamWriter writer = new System.IO.StreamWriter(Save.FileName);
            writer.WriteLine(savestring);
            foreach (String item in ItemList.Items)
            {
                writer.WriteLine(item.ToString());
            }
            filename = Save.FileName;
            writer.Close();
        }

        private void SaveButton_Click(System.Object sender, System.EventArgs e)
        {
            if (filename == "untitled")
            {
                SaveAs();
            }
            else
            {
                System.IO.StreamWriter stream = new System.IO.StreamWriter(filename);
                stream.WriteLine(savestring);
                foreach (object item in ItemList.Items)
                {
                    stream.WriteLine(item.ToString());
                }
                stream.Close();
            }
        }
        private void SaveAsButton_Click(System.Object sender, System.EventArgs e)
        {
            SaveAs();
        }

        private void InsertText_TextChanged(System.Object sender, System.EventArgs e)
        {
            if (string.IsNullOrEmpty(InsertText.Text))
            {
                AddButton.Enabled = false;
            }
            else
            {
                AddButton.Enabled = true;
            }
        }

        private void Exit_Click(System.Object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void PropertiesButton_Click(System.Object sender, System.EventArgs e)
        {
            Forms.Stats stats = new Forms.Stats();
            stats.ShowDialog();
        }
    }
}
